﻿/*
 * @author: wizardc
 */

namespace Dou.Singleton
{
    /// <summary>
    /// 任意类型的单例实现
    /// <example>
    /// <![CDATA[
    /// // 通过继承单例基类
    /// public class MyClass : Singleton<MyClass>{ }
    /// MyClass.instance.DoSomething
    /// // 不继承单例基类
    /// public class MyClass { }
    /// Singleton<MyClass>.instance.DoSomething
    /// ]]>
    /// </example>
    /// </summary>
    public class Singleton<T> where T : new()
    {
        private static T _instance;

        private static readonly object _lockObj = new object();

        public static T instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new T();
                        }
                    }
                }
                return _instance;
            }
        }
    }
}
